package com.onlyforfun.exchange.view;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.onlyforfun.exchange.R;
import com.onlyforfun.exchange.util.CurrencyConverter;

public class SortArrayAdapter extends ArrayAdapter<String> {
	private final List<String> list;
	public List<String> getList() {
		return list;
	}

	private final Activity context;
	int layoutResourceId;

	public SortArrayAdapter(Activity context, int layoutResourceId, List<String> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.list = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		TimePriceHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TimePriceHolder();
			holder.name = (TextView) row
					.findViewById(R.id.name);
			holder.country = (ImageView) row.findViewById(R.id.drag_handle);
			
			row.setTag(holder);
		} else {
			holder = (TimePriceHolder) row.getTag();
		}

		String name =  list.get(position);
		holder.name.setText(name);
		holder.country.setImageResource(CurrencyConverter.getIconByName(name));
		return row;
	}

	static class TimePriceHolder {
		ImageView country;
		TextView name;
	}
}
